package com.axjy.web;

import com.axjy.pojo.LineInfo;
import com.axjy.service.LineInfoService;
import com.axjy.service.PoleInfoService;
import com.axjy.service.vo.Pager;
import com.axjy.util.Log;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by l on 2018/2/1.
 */
@RestController
@RequestMapping("/line")
public class LineInfoHandler {

    @Resource
    private LineInfoService lineInfoService;
    @Resource
    private PoleInfoService poleInfoService;

    /**
     * 模糊查询
     * @param offset
     * @param limit
     * @param lineCode
     * @param runningStatusName
     * @return
     */
    @GetMapping("/mhSearch")
    public Pager mhSearch(@RequestParam(required = false,defaultValue = "0") Integer offset,@RequestParam(required = false,defaultValue = "1") Integer limit,String lineCode,String runningStatusName){
        Pager pager=lineInfoService.mhSearch(offset,limit,lineCode,runningStatusName);
        return pager;
    }

    @GetMapping("/search")
    public List<LineInfo> search(){
        return lineInfoService.search();
    }

    /**
     * 新增线路
     * @param line
     * @return
     */
    @PostMapping("/addLine")
    public String addLine(LineInfo line,Integer isStart){
        String msg=null;
        if (isStart==1){
            line.setRunningStatusName("正常");
        }else{
            line.setRunningStatusName("检修中");
        }
        try{
            lineInfoService.addLine(line);
            msg="新增成功";
        }catch (RuntimeException ex){
            msg="新增失败";
            Log.logger.error(msg,ex);
        }
        return msg;
    }

    /**
     * 删除线路
     * @param lineCode
     * @return
     */
    @Transactional
    @PostMapping("/delLine")
    public String delLine(String lineCode){
        String msg=null;
        try{
            lineInfoService.delLine(lineCode);
            poleInfoService.emptyDate(lineCode);
            msg="删除成功";
        }catch (RuntimeException ex){
            msg="删除失败";
            Log.logger.error(msg,ex);
        }
        return msg;
    }

    /**
     * 修改线路
     * @param line
     * @return
     */
    @PostMapping("/update")
    public String update(LineInfo line){
        String msg=null;
        try{
            msg="修改成功";
            lineInfoService.updateLine(line);
        } catch (RuntimeException ex){
            msg="修改失败";
            Log.logger.error(msg,ex);
        }
        return msg;
    }

    /**
     * 修改状态
     * @param id
     * @param isStart
     * @return
     */
    @PostMapping("/updateStart")
    public String updateStart(Integer id,Integer isStart,Integer runningStatus,String runningStatusName,String lineCode){
        String msg=null;
        try{
            lineInfoService.updateStart(id,isStart,runningStatus,runningStatusName);
            poleInfoService.updateStart(lineCode,isStart);
            msg="修改状态成功";
        } catch (RuntimeException ex){
            msg="修改状态失败";
            Log.logger.error(msg,ex);
        }
        return msg;
    }
}
